home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / docs / splittmpl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  3.1 KB  |  129 lines

  1. /**************************************************************
  2.  * Split the tmpl file in LaTeX and help files depending on flag.
  3.  * It support the binary syntax @ifhelp, @else, @endif
  4.  * as well as @iftex, @else, @endif in the input file.
  5.  *
  6.  * Martin-D. Lacasse 1992
  7.  *
  8.  **************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define MAXLVL 2  /* Keep it simple! */
  14.  
  15. main(int argc, char **argv)
  16. {
  17.  
  18.     FILE *fpin[MAXLVL], *fpout=stderr;
  19.     char bufline[1024];
  20.     char filename[256];
  21.     int lineno[MAXLVL];
  22.     int inhelp = 1;
  23.     int intex = 1;
  24.     int tex = 0;
  25.     int help = 0;
  26.     int fplvl = 0;
  27.     int total = 0;
  28.     char *ci, *co;
  29.  
  30.     if (argc != 3) {
  31.         fprintf(stderr, "Usage: %s [-tex|help] infile\n", argv[0]);
  32.         exit(1);
  33.     }
  34.     co = bufline;
  35.     ci = argv[2];
  36.     while (*ci != '.' && *ci) 
  37.         *co++ = *ci++;
  38.     *co = '\0';
  39.     if ((fpin[fplvl] = fopen(argv[2], "r")) == NULL) {
  40.         fprintf(stderr, "%s: %s: File not found\n", argv[0], argv[2]);
  41.         exit(1);
  42.     }
  43.     if (strcmp(argv[1], "-tex") == 0) {
  44.         sprintf(filename, "%s.tex", bufline);
  45.         if ((fpout = fopen(filename, "w")) == NULL) {
  46.             fprintf(stderr, "%s: Permission denied.\n", filename);
  47.             exit(1);
  48.         }
  49.         tex = 1;
  50.     }
  51.     else if (strcmp(argv[1], "-help") == 0) {
  52.         sprintf(filename, "%s.help", bufline);
  53.         if ((fpout = fopen(filename, "w")) == NULL) {
  54.             fprintf(stderr, "%s: Permission denied.\n", filename);
  55.             exit(1);
  56.         }
  57.         help = 1;
  58.     }
  59.     else {
  60.         fprintf(stderr, "Usage: %s [-tex|help] infile\n", argv[0]);
  61.         exit(1);
  62.     }
  63.         
  64.     fplvl = 0;
  65.     lineno[fplvl] = 0;
  66.     while (fplvl >= 0) {
  67.         if (fgets(bufline, 1023, fpin[fplvl]) == NULL) {
  68.             fclose(fpin[fplvl]);
  69.             if (fplvl) {
  70.                 fprintf(stderr, "%d lines.\n", lineno[fplvl]);
  71.                 total += lineno[fplvl];
  72.             }
  73.             else {
  74.                 fprintf(stderr, "%s: %d lines.\n", argv[2], lineno[0]);
  75.                 total += lineno[fplvl];
  76.                 fprintf(stderr, "Total: %d lines.\n", total);
  77.             }
  78.             fplvl--;
  79.             continue;
  80.         }
  81.         lineno[fplvl]++;
  82.         if (bufline[0] == '@') {
  83.             if (strncmp(bufline+1, "ifhelp", 6) == 0) {
  84.                 inhelp = 1; intex = 0;
  85.             }
  86.             else if (strncmp(bufline+1, "iftex", 5) == 0) {
  87.                 inhelp = 0; intex = 1;
  88.             }
  89.             else if (strncmp(bufline+1, "else", 4) == 0) {
  90.                 inhelp = !inhelp; intex = !intex;
  91.             }
  92.             else if (strncmp(bufline+1, "endif", 5) == 0) {
  93.                 inhelp = 1; intex = 1;
  94.             }
  95.             else if (strncmp(bufline+1, "include", 7) == 0) {
  96.                 if ((tex && intex) || (help && inhelp)) {
  97.                     if (++fplvl > MAXLVL-1) {
  98.                         fprintf(stderr, "Error: Level %d: Line %d: %s.\n",
  99.                         fplvl, lineno[fplvl-1], "Too many @include");
  100.                         exit(1);
  101.                     }
  102.                     lineno[fplvl] = 0;
  103.                     sscanf(bufline+8, "%s", filename);
  104.                     if ((fpin[fplvl] = fopen(filename, "r")) == NULL) {
  105.                         fprintf(stderr,
  106.                         "%s: %s: File not found\n", argv[0], filename);
  107.                         exit(1);
  108.                     }
  109.                     fprintf(stderr, "%s: ", filename);
  110.                 }
  111.                 continue;
  112.             }
  113.             else {
  114.                 fprintf(stderr, "Error: Line %d: %s.\n", lineno, bufline);
  115.                 exit(1);
  116.             }
  117.             continue;
  118.         }
  119.         if ((tex && intex) || (help && inhelp))
  120.             fputs(bufline, fpout);
  121.     }
  122.     if (!(intex * inhelp)) {
  123.         fprintf(stderr, "Exiting with an open if statement.\n");
  124.         exit(1);
  125.     }
  126.     exit(0);
  127. }
  128.  
  129.